package com.bootdo.crm.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.crm.domain.BusinessTypeDO;
import com.bootdo.crm.service.BusinessTypeService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 商机状态组类别
 * 
 * @author cui
 * @email 1992lcg@163.com
 * @date 2020-02-20 13:14:28
 */
 
@Controller
@RequestMapping("/crm/businessType")
public class BusinessTypeController {
	@Autowired
	private BusinessTypeService businessTypeService;
	
	@GetMapping()
	@RequiresPermissions("crm:businessType:businessType")
	String BusinessType(){
	    return "crm/businessType/businessType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("crm:businessType:businessType")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BusinessTypeDO> businessTypeList = businessTypeService.list(query);
		int total = businessTypeService.count(query);
		PageUtils pageUtils = new PageUtils(businessTypeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("crm:businessType:add")
	String add(){
	    return "crm/businessType/add";
	}

	@GetMapping("/edit/{typeId}")
	@RequiresPermissions("crm:businessType:edit")
	String edit(@PathVariable("typeId") Integer typeId,Model model){
		BusinessTypeDO businessType = businessTypeService.get(typeId);
		model.addAttribute("businessType", businessType);
	    return "crm/businessType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("crm:businessType:add")
	public R save( BusinessTypeDO businessType){
		if(businessTypeService.save(businessType)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("crm:businessType:edit")
	public R update( BusinessTypeDO businessType){
		businessTypeService.update(businessType);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("crm:businessType:remove")
	public R remove( Integer typeId){
		if(businessTypeService.remove(typeId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("crm:businessType:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] typeIds){
		businessTypeService.batchRemove(typeIds);
		return R.ok();
	}
	
}
